package com.cf.auth.config;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.cf.auth.module.RoleInfo;
import com.cf.auth.module.SwordGrantedAuthority;
import com.cf.auth.module.SwordUserDetails;
import com.cf.auth.module.UserInfo;
import com.cf.auth.utils.TokenUtil;
import com.cf.common.constant.AuthConstant;
import lombok.AllArgsConstructor;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 自定义用户信息查询
 * @author ChenFeng
 * @date 2021/11/8 2:41 下午
 * @description
 */
@Component
@AllArgsConstructor
public class SwordUserDetailsService implements UserDetailsService {

//    private final UserFeign userFeign;

    private final JdbcTemplate jdbcTemplate;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        if (StringUtils.isBlank(username)) {
            throw new UsernameNotFoundException("the username is not null");
        }
        UserInfo userInfo = jdbcTemplate.queryForObject(AuthConstant.BASE_USER_STATEMENT + " where is_deleted = 0 and account=?", new BeanPropertyRowMapper<>(UserInfo.class), username);
        if (null == userInfo) {
            throw new UsernameNotFoundException("the user is not exist");
        }
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = ((ServletRequestAttributes)requestAttributes).getRequest();
        // 获取用户绑定ID
        String headerDept = request.getHeader(TokenUtil.DEPT_HEADER_KEY);
        String headerRole = request.getHeader(TokenUtil.ROLE_HEADER_KEY);
        // 获取租户ID
        String headerTenant = request.getHeader(TokenUtil.TENANT_HEADER_KEY);
        String paramTenant = request.getParameter(TokenUtil.TENANT_PARAM_KEY);
        String password = request.getParameter(TokenUtil.PASSWORD_KEY);
        String grantType = request.getParameter(TokenUtil.GRANT_TYPE_KEY);

        List<RoleInfo> roles = jdbcTemplate.query(AuthConstant.BASE_ROLE_STATEMENT + " where is_deleted = 0 and id= ?", new BeanPropertyRowMapper<>(RoleInfo.class), userInfo.getRoleId());
        List<SwordGrantedAuthority> swordGrantedAuthorities = roles.stream().map(role -> {
            SwordGrantedAuthority swordGrantedAuthority = new SwordGrantedAuthority();
            swordGrantedAuthority.setAuthority(role.getRoleAlias());
            return swordGrantedAuthority;
        }).collect(Collectors.toList());
        SwordUserDetails swordUserDetails = new SwordUserDetails();
        swordUserDetails.setUserId(userInfo.getId().toString());
        swordUserDetails.setDeptId(userInfo.getDeptId());
        swordUserDetails.setRoleId(userInfo.getRoleId());
        swordUserDetails.setPostId(userInfo.getPostId());
        swordUserDetails.setAccount(username);
        swordUserDetails.setRealName(username);
        swordUserDetails.setTenantId(headerTenant);
        swordUserDetails.setUsername(username);
        swordUserDetails.setAuthorities(swordGrantedAuthorities);
        swordUserDetails.setPassword(AuthConstant.ENCRYPT_PASSWORD + userInfo.getPassword());
        return swordUserDetails;
    }
}
